package com.example.huhu.ch04_toast;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //根据ｉｄ获取组件
        textView = (TextView) findViewById(R.id.text01);
        btn = (Button) findViewById(R.id.button01);
        //获取Builder对象
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.setMessage("真的要删除该记录吗？")
                        .setPositiveButton("是",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        textView.setText("删除成功");
                                        Toast t1 = Toast.makeText(getApplicationContext(),"删除成功",
                                                Toast.LENGTH_LONG);
                                        t1.show();
                                    }
                                })
                        .setNegativeButton("否",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        textView.setText("取消删除");
                                        Toast t2 = Toast.makeText(getApplicationContext(),"取消删除",
                                                Toast.LENGTH_LONG);
                                        t2.show();
                                    }
                                }
                        );
                AlertDialog ad = builder.create();
                ad.show();


            }
        });
    }
}
